package com.doubledr.light_lock;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.doubledr.light_lock.Task.DecryptTask;
import com.doubledr.light_lock.utils.FileManager;
import com.doubledr.light_lock.utils.PathManager;
import com.doubledr.light_lock.utils.ShareRefHelper;

import java.io.File;

public class TextEncryptActivity extends AppCompatActivity {
    private Button mEncryptBtn;
    private Button mCopyBtn;
    private ImageButton mBackBtn;
    private TextView mInputText;
    private TextView mOuputText;
    private TextView mTip;
    private TextView mTitle;

    private String mType;
    private int mPwd = 0;
    private Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_encrypt);
        Intent intent=getIntent();
        mType = intent.getStringExtra("type");
        String pwd = ShareRefHelper.getValue(ShareRefHelper.getFileID());
        if(!pwd.contentEquals("") && pwd!=null){
            mPwd = Integer.parseInt(pwd);
        }
        mContext = this;
        initViews();
        bindEvents();
    }
    private void initViews(){
        mEncryptBtn = findViewById(R.id.btnStartEncrypt);
        mCopyBtn = findViewById(R.id.btnCopy);
        mBackBtn = findViewById(R.id.btnBack);
        mInputText = findViewById(R.id.txtInput);
        mOuputText = findViewById(R.id.txtOutput);
        mTip = findViewById(R.id.txtTip);
        mTitle = findViewById(R.id.txtTitle);

        if(mType.equalsIgnoreCase("EN")){
            mEncryptBtn.setText(R.string.txt_en_txt);
            mTip.setText(R.string.en_result);
            mTitle.setText(R.string.please_input_en_txt);
        }
        else{
            mEncryptBtn.setText(R.string.txt_decry_txt);
            mTip.setText(R.string.de_result);
            mTitle.setText(R.string.please_input_de_txt);
        }

    }
    private void bindEvents(){
        mEncryptBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mType.equalsIgnoreCase("EN")){
                    //开始对文本进行加密
                    String orgTxt = mInputText.getText().toString();
                    if(orgTxt.trim().equalsIgnoreCase("")){
                        Toast toast=Toast.makeText(TextEncryptActivity.this, R.string.en_txt_cannot_null,Toast.LENGTH_SHORT    );
                        toast.setGravity(Gravity.BOTTOM, 0, 0);
                        toast.show();
                    }
                    else{
                        //开始文本加密
                        String rs = FileManager.encryptTxt(orgTxt,mPwd);
                        mOuputText.setText(rs);
                    }
                }
                else {
                    //开始对文本进行解密

                    final String orgTxt = mInputText.getText().toString();
                    if(orgTxt.trim().equalsIgnoreCase("")){
                        Toast toast=Toast.makeText(TextEncryptActivity.this, R.string.de_txt_cannot_null,Toast.LENGTH_SHORT    );
                        toast.setGravity(Gravity.BOTTOM, 0, 0);
                        toast.show();
                    }
                    else{
                        android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(mContext);
                        builder.setTitle(R.string.input_pwd);
                        View view = getLayoutInflater().inflate(R.layout.alert_input_layout,null);
                        final TextView txtInput = view.findViewById(R.id.edtPwdRq);
                        builder.setView(view);
                        builder.setCancelable(false);
                        builder.setPositiveButton(R.string.sure, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String newPwd = txtInput.getText().toString();
                                if(newPwd.trim().contentEquals("")){
                                    Toast toast=Toast.makeText(mContext, R.string.pwd_cannot_null,Toast.LENGTH_SHORT    );
                                    toast.setGravity(Gravity.CENTER, 0, 0);
                                    toast.show();
                                    return;
                                }
                                int pwd = Integer.parseInt(newPwd);
                                //开始文本解密
                                String rs = FileManager.decryptTxt(orgTxt,pwd);
                                mOuputText.setText(rs);
                            }
                        });
                        builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                        builder.show();
                    }
                }

            }
        });
        mCopyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String enTxt = mOuputText.getText().toString();
                ClipboardManager cmb = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData clipData = ClipData.newPlainText("Label", enTxt);
                cmb.setPrimaryClip(clipData);
                Toast toast=Toast.makeText(TextEncryptActivity.this, R.string.already_copied,Toast.LENGTH_SHORT    );
                toast.setGravity(Gravity.BOTTOM, 0, 0);
                toast.show();
            }
        });
        mBackBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               TextEncryptActivity.this.finish();
            }
        });
    }
}
